package cn.e3mall.search.service.impl;

import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.e3mall.common.pojo.SearchResult;
import cn.e3mall.search.dao.SearchDao;
import cn.e3mall.search.service.SearchService;

/*
 * 商品搜索Service
 */
@Service
public class SearchServiceImpl implements SearchService {

	@Autowired
	private SearchDao searchdao;
	
	@Override
	public SearchResult search(String keyword, int page, int rows) throws Exception {
		SolrQuery query= new SolrQuery();
		query.setQuery(keyword);
		if (page<=0) {
			page=1;
		}
		//设置分页条件
		query.setStart((page-1)*rows);
		query.setRows(rows);
		//设置默认排序
		query.set("df", "item_title");
		//开启高亮显示
		query.setHighlight(true);
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<em style=\"color:red\">");
		query.setHighlightSimplePost("</em>") ;
		SearchResult searchResult = searchdao.searchResult(query);
		//通过总记录数获取总页数
		long recordCount = searchResult.getRecordCount();
		int totalPage=(int)(recordCount%rows==0
				?recordCount/rows:recordCount/rows+1);
		searchResult.setTotalPages(totalPage);
		
		return searchResult;
	}

}
